package com.shine.app.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.adapter.MyReviewAdapter;
import com.shine.app.bean.MyReviewBean;
import com.shine.app.bean.UserBean;
import com.shine.app.commons.Commons;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfoReviewActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;
	@InjectView(R.id.head_number)
	private TextView head_numberTextView;
	@InjectView(R.id.listview)
	private ListView listView;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private String number = Commons.ZERO;
	private GetMyReviewsAsyncTask getMyReviewsAsyncTask;
	private MyReviewAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewmyinfo);
		Injector.get(this).inject();
		init();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void init() {
		List<MyReviewBean> list = new ArrayList<MyReviewBean>();
		adapter = new MyReviewAdapter(this, list, imageLoader);
		listView.setAdapter(adapter);
		head_nameTextView.setText(R.string.view_reviews);
		head_backImageView.setOnClickListener(this);
		listView.setOnItemClickListener(this);
	}

	private void initData() {
		UserBean user = appContext.getLoginInfo().getUser();
		getMyReviewsAsyncTask = new GetMyReviewsAsyncTask();
		getMyReviewsAsyncTask.execute(user.getGolferId());
	}

	public class GetMyReviewsAsyncTask extends
			AsyncTask<Object, Integer, List<MyReviewBean>> {
		private String golferId;

		@Override
		protected List<MyReviewBean> doInBackground(Object... params) {
			List<MyReviewBean> list = null;
			golferId = (String) params[0];
			list = appContext.getMyReviewsByGolfer(golferId);
			if (list == null) {
				list = new ArrayList<MyReviewBean>();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<MyReviewBean> result) {
			super.onPostExecute(result);
			number = String.valueOf(result.size());
			head_numberTextView.setText("(" + number + ")");
			progressBar.setVisibility(View.GONE);
			adapter.setData(result);
			adapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		AppManager.getAppManager().finishActivity();
		overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
		TextView idTextView = (TextView) view.findViewById(R.id.id);
		MyReviewBean bean = (MyReviewBean) idTextView.getTag();
		if (bean == null) {
			return;
		}
		Intent intent = new Intent(this, ViewMyInfoReview_detailActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("number", number);
		intent.putExtra("bean", bean);
		this.startActivity(intent);
		overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
	}

	@Override
	protected void onDestroy() {
		if (getMyReviewsAsyncTask != null
				&& getMyReviewsAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			getMyReviewsAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
